CheckBox = {}

function CheckBox.init (nbr, x, y, text, genre, couleurCoche)
  local nb = tostring (nbr)
  _G[nb] = {}
  _G[nb].x1 = x -- 47
  _G[nb].y1 = y -- 50
  _G[nb].x2 = x+8
  _G[nb].y2 = y+8
  _G[nb].stxwx = x+13
  _G[nb].stxwy = y
  _G[nb].text = text
  _G[nb].bxx = 0
  _G[nb].bxy = 0
  _G[nb].bx = newBox (_G[nb].x1, _G[nb].y1, _G[nb].stxwx+string.len (_G[nb].text)*6, _G[nb].y2)
  _G[nb].c1 = c_noir
  _G[nb].c2 = c_blanc
  _G[nb].sel = false
  _G[nb].genre = genre
  if couleurCoche == nil then
    _G[nb].cochCol = c_blanc
  else
    _G[nb].cochCol = couleurCoche 
  end
end

function CheckBox.draw (nbr)
  local nb = tostring (nbr)
  if Stylus.held then
    _G[nb].bxx = Stylus.X
    _G[nb].bxy = Stylus.Y
  end
  screen.print (SCREEN_DOWN, _G[nb].stxwx, _G[nb].stxwy, _G[nb].text)
  screen.drawFillRect (SCREEN_DOWN, _G[nb].x1, _G[nb].y1, _G[nb].x2, _G[nb].y2, _G[nb].c1)
  screen.drawRect (SCREEN_DOWN, _G[nb].x1, _G[nb].y1, _G[nb].x2, _G[nb].y2, c_blanc)
  if _G[nb].genre == 'crossInBox' then
    if _G[nb].sel == true then
      screen.print (SCREEN_DOWN, _G[nb].x1+1, _G[nb].y1, "/", _G[nb].cochCol)
      screen.print (SCREEN_DOWN, _G[nb].x1, _G[nb].y1, "\\", _G[nb].cochCol)
      screen.drawFillRect (SCREEN_DOWN, _G[nb].x1+1, _G[nb].y2-2, _G[nb].x1+2, _G[nb].y2-1, _G[nb].cochCol)
      screen.drawFillRect (SCREEN_DOWN, _G[nb].x2-1, _G[nb].y2-2, _G[nb].x2-2, _G[nb].y2-1, _G[nb].cochCol)
    end
  elseif _G[nb].genre == 'vInBox' then
    if _G[nb].sel == true then
      screen.print (SCREEN_DOWN, _G[nb].x1+1, _G[nb].y1+1, "", _G[nb].cochCol)
    end
  end
  if Stylus.newPressinBox (_G[nb].bx, _G[nb].bxx, _G[nb].bxy) and _G[nb].sel == false then
    if _G[nb].genre == 'fillInBox' then
      _G[nb].c1 = _G[nb].cochCol
    else
      _G[nb].c1 = c_noir
    end
    _G[nb].c2 = c_noir
    _G[nb].sel = true
  elseif Stylus.newPressinBox (_G[nb].bx, _G[nb].bxx, _G[nb].bxy) and _G[nb].sel == true then
    _G[nb].c1 = c_noir
    _G[nb].c2 = c_blanc
    _G[nb].sel = false
  end
end

function CheckBox.selected (nbr)
  local nb = tostring (nbr)
  
  return _G[nb].sel
end

function CheckBox.clear (nbr)
  local nb = tostring (nbr)
  
  _G[nb] = nil
  
  chkbxx = 0
  chkbxy = 0
end
